package com.hualan.sfzy.service.counselor.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hualan.sfzy.constant.CommonConstant;
import com.hualan.sfzy.entity.counselor.CounselorTalent;
import com.hualan.sfzy.entity.counselor.Talent;
import com.hualan.sfzy.mapper.counselor.CounselorTalentMapper;
import com.hualan.sfzy.mapper.counselor.TalentMapper;
import com.hualan.sfzy.service.counselor.TalentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("TalentService")
public class TalentServiceImpl implements TalentService {
    @Autowired
    private TalentMapper talentMapper;
    @Autowired
    private CounselorTalentMapper counselorTalentMapper;

    @Override
    public int add(Talent talent) {
        return talentMapper.insert(talent);
    }

    @Override
    @Transactional
    public int remove(Integer id) {
        CounselorTalent counselorTalent = new CounselorTalent();
        counselorTalent.setTalentId(id);
        List<CounselorTalent> list = counselorTalentMapper.select(counselorTalent);
        if (list.size() > 0) {
            throw new RuntimeException("该擅长领域已有咨询师使用");
        }
        return talentMapper.delete(id);
    }

    @Override
    @Transactional
    public int removeBatch(Integer[] ids) {
        for (Integer id : ids) {
            CounselorTalent counselorTalent = new CounselorTalent();
            counselorTalent.setTalentId(id);
            List<CounselorTalent> list = counselorTalentMapper.select(counselorTalent);
            if (list.size() > 0) {
                throw new RuntimeException("该擅长领域已有咨询师使用");
            }
        }
        return talentMapper.deleteBatch(ids);
    }

    @Override
    public int edit(Talent talent) {
        return talentMapper.update(talent);
    }

    @Override
    public List<Talent> find() {
        return talentMapper.select(null);
    }

    @Override
    public Map<String, Object> findByPage(Integer pageNum, Integer pageSize, Talent entity) {
        PageHelper.startPage(pageNum,pageSize);

        List<Talent> list = talentMapper.select(entity);

        PageInfo<Talent> pageInfo = new PageInfo<>(list);

        Map<String, Object> map = new HashMap<>();

        map.put(CommonConstant.PAGE_LIST,list);
        map.put(CommonConstant.PAGE_TOTAL,pageInfo.getPages());
        map.put(CommonConstant.TOTAL,pageInfo.getTotal());

        return map;
    }

    @Override
    public Talent findById(Integer id) {
        return talentMapper.selectById(id);
    }

}
